package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Color;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JPanel;
import model.BSSingleton;
import model.TombStone;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class Order implements ActionListener
{
    private JPanel 
            orderPanel,
            prevNext;
    private JButton 
            btnPrev,
            btnNext,
            btnReset,
            btnAdd;
    private OrderContainer orderContainer;
    private int currentActivePanel;
    private ArrayList<JPanel> panelShowing;
    private model.Order orderObject;
    private OrderOverview orderOverview;

    public Order(OrderContainer view)
    {
        orderContainer = view;
        
        currentActivePanel = -1;
        panelShowing = new ArrayList();
        
        orderPanel = new JPanel(null);
        orderPanel.setBounds(0, 0, 900, 500);
        orderPanel.setOpaque(false);
        
        orderOverview = new OrderOverview(orderContainer);
        orderOverview.getPanel().setVisible(true);
        
        JPanel whiteBG = new JPanel(null);
        whiteBG.setBounds(322, 12, 574, 486);
        whiteBG.setBackground(Color.WHITE);
        
        prevNext = new JPanel(null);
        prevNext.setBounds(0, 465, 300, 30);
        prevNext.setVisible(false);
        
        btnPrev = new JButton("tilbage");
        btnPrev.setBounds(3, 0, 100, 30);
        btnPrev.setBorder(DesignLayout.T_FIELD_RED);
        btnPrev.setFont(DesignLayout.STANDARD.deriveFont(12f));
        btnPrev.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnPrev.setEnabled(false);
        btnPrev.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                showPrevPanel();
            }
        });
        
        btnReset = new JButton("genstart");
        btnReset.setBounds(110, 0, 80, 30);
        btnReset.setBorder(DesignLayout.T_FIELD_RED);
        btnReset.setFont(DesignLayout.STANDARD.deriveFont(12f));
        btnReset.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnReset.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                restart();
            }
        });
        
        btnNext = new JButton("videre");
        btnNext.setBounds(197, 0, 100, 30);
        btnNext.setBorder(DesignLayout.T_FIELD_RED);
        btnNext.setFont(DesignLayout.STANDARD.deriveFont(12f));
        btnNext.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnNext.setEnabled(false);
        btnNext.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                showNextPanel();
            }
        });
        
        btnAdd = new JButton("gem ordre");
        btnAdd.setBounds(197, 0, 100, 30);
        btnAdd.setBorder(DesignLayout.T_FIELD_RED);
        btnAdd.setFont(DesignLayout.STANDARD.deriveFont(12f));
        btnAdd.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnAdd.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    if (orderObject.getId() > 0)
                    {
                        // Altering not implemented yet
                    }
                    else
                    {
                        boolean added = control.OrderControl.addOrder(orderObject);
                        if (added == true)
                        {
                            restart();
                        }
                    }
                } 
                catch (SQLException ex)
                {
                    ExceptionManager.handleSQLException(ex);
                } 
                catch (ClassNotFoundException ex)
                {
                    ExceptionManager.handleClassNotFoundException(ex);
                }
            }
        });
        
        prevNext.add(btnPrev);
        prevNext.add(btnReset);
        prevNext.add(btnNext);
        prevNext.add(btnAdd);
        
        orderPanel.add(prevNext);
        orderPanel.add(orderOverview.getPanel());
        orderPanel.add(whiteBG);
        orderPanel.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if (orderObject.getOrderLine().size() > 0)
        {
            btnAdd.setEnabled(true);
        }
        else
        {
            btnAdd.setEnabled(false);
        }
        updateViews();
    }
    
    private void createOrderObject()
    {
        orderObject = null;
        try
        {
            orderObject = new model.Order();            
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
    }
    
    public void restart()
    {
        createOrderObject();
        currentActivePanel = 0;
        resetOrderViews();
        updateViews();
        
        orderPanel.setVisible(false);
        orderContainer.getOrderSearch().getPanel().setVisible(true);
        orderContainer.getOrderSearchList().getPanel().setVisible(true);
        orderContainer.getNewOrderButtonPanel().getPanel().setVisible(true);
        orderContainer.getOrderSearchList().buildExistingScrollPanel("WHERE finished = 0", "ORDER BY orderID DESC", "LIMIT 10");
    }
    
    public void updateViews()
    {
        if (currentActivePanel == 0)
        {
            btnPrev.setEnabled(false);
            if ( 
                (orderObject.getCustomerSpecified()[0].getCustomerTelephone().isCompleted() 
                    && 
                orderObject.getCustomerSpecified()[1].getCustomerTelephone().isCompleted()))
            {
                btnNext.setEnabled(true);
            }
        }
        
        orderOverview.buildOverviewPanel();
    }
    
    public void resetOrder(boolean tombstone)
    {
        createOrderObject();
        try
        {
            orderObject.reset();
            if (tombstone)
            {
                orderObject.setTombStone(new TombStone());
            }
            orderObject.setUser(BSSingleton.getSingletonObject().getLoggedInUser());
            orderObject.addListener(this);
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        resetOrderViews();
        updateViews();
    }
    
    public void showOrder(model.Order order)
    {
        this.orderObject = order;
        resetOrderViews();
        updateViews();
    }
    
    private void resetOrderViews()
    {
        // Hvis vi allerede har været her én gang, skal panelerne også resettes.
        // Ellers vil tidligere søgeresultater ses.
        for (JPanel value : panelShowing)
        {
            orderPanel.remove(value);
        }
        panelShowing.clear();
        btnNext.setVisible(true);
        btnAdd.setVisible(false);
        
        // Search by telephone
        OrderCustomer orderCustomer = new OrderCustomer(this);              
        orderPanel.add(orderCustomer.getPanel());
        panelShowing.add(orderCustomer.getPanel());
        ViewSingleton.getSingletonObject().setOrderCustomer(orderCustomer);

        // Pickup and Delivery date
        OrderDate orderDate = new OrderDate(this);
        orderDate.setFieldsFromModel();
        orderPanel.add(orderDate.getPanel());
        panelShowing.add(orderDate.getPanel());

        if (orderObject.getTombStone() != null)
        {
            // Inscription and other stuff
            OrderInscription orderInscription = new OrderInscription(this);
            orderInscription.setFieldsFromModel();
            orderPanel.add(orderInscription.getPanel());
            panelShowing.add(orderInscription.getPanel());

            // GraveYard
            OrderGraveYard orderGraveYard = new OrderGraveYard(this);
            orderGraveYard.setFieldsFromModel();
            orderPanel.add(orderGraveYard.getPanel());
            panelShowing.add(orderGraveYard.getPanel());
            ViewSingleton.getSingletonObject().setOrderGraveYard(orderGraveYard);
        }

        // Product
        OrderAddProduct orderAddProduct = new OrderAddProduct(this);
        orderPanel.add(orderAddProduct.getPanel());
        panelShowing.add(orderAddProduct.getPanel());

        // Comment
        OrderComment orderComment = new OrderComment(this);
        orderComment.setFieldsFromModel();
        orderPanel.add(orderComment.getPanel());
        panelShowing.add(orderComment.getPanel());

        currentActivePanel = -1;
        showNextPanel();
        
        orderPanel.revalidate();
        orderPanel.repaint();        
    }
    
    private void hideAllPanels()
    {
        for (JPanel value : panelShowing)
        {
            value.setVisible(false);
        }
    }
    
    private void showNextPanel()
    {
        if (panelShowing.size() -1 > currentActivePanel)
        {
            currentActivePanel++;
            hideAllPanels();
            panelShowing.get(currentActivePanel).setVisible(true);
            prevNext.setVisible(true);
            
            if (currentActivePanel > 0)
            {
                btnPrev.setEnabled(true);
            }
            
            if (panelShowing.size()-1 > currentActivePanel)
            {
                btnNext.setEnabled(true);
            }
            else
            {
                btnNext.setEnabled(false);
                btnNext.setVisible(false);
                btnAdd.setVisible(true);
            }
            
            if (currentActivePanel == 0)
            {
                if ( 
                    (orderObject.getCustomerSpecified()[0].getCustomerTelephone().isCompleted() 
                        && 
                    orderObject.getCustomerSpecified()[1].getCustomerTelephone().isCompleted()))
                {
                    btnNext.setEnabled(true);
                }
                else
                {
                    btnNext.setEnabled(false);
                }
            }
        }
    }
    
    private void showPrevPanel()
    {
        if (currentActivePanel > 0)
        {
            currentActivePanel--;
            hideAllPanels();
            panelShowing.get(currentActivePanel).setVisible(true);
        }
        
        if (currentActivePanel < panelShowing.size()-1 && 
                (orderObject.getCustomerSpecified()[0].getCustomerTelephone().isCompleted() 
                    && 
                orderObject.getCustomerSpecified()[1].getCustomerTelephone().isCompleted()))
        {
            btnNext.setEnabled(true);
        }
        else
        {
            btnNext.setEnabled(false);
        }
        
        if (currentActivePanel == 0)
        {
            btnPrev.setEnabled(false);
        }
        
        btnNext.setVisible(true);
        btnAdd.setVisible(false);
    }

    public OrderOverview getOrderOverview()
    {
        return orderOverview;
    }

    public OrderContainer getOrderContainer()
    {
        return orderContainer;
    }

    public model.Order getOrderObject()
    {
        return orderObject;
    }
    
    public void setOrderObject(model.Order order)
    {
        orderObject = order;
        updateViews();
    }

    public JPanel getPanel()
    {
        return orderPanel;
    }
}